package com.atguigu.myjuc.rwlock;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLockDemo2 {

    public static void main(String[] args) {
        ReadWriteLock rwLock = new ReentrantReadWriteLock();
        rwLock.writeLock().lock();
        System.out.println("获取到写锁======写");
        rwLock.readLock().lock();
        System.out.println("获取到写锁======写");
        //读数据
        //可以插入其他的读线程（因为读读可并发），从而提高应用程序的执行效率
        rwLock.writeLock().unlock();
        System.out.println("释放读锁==========");
        rwLock.readLock().lock();
        System.out.println("释放读锁==========");





        /*//错误的锁降级示例，因为在释放写锁和获取读锁之间能够有其他的写线程插入，从而影响数据的一致性

        rwLock.writeLock().lock();
        System.out.println("获取到写锁======写");
        System.out.println("释放======写");
        rwLock.writeLock().unlock();


        //可能插入其他写线程

        rwLock.readLock().lock();
        System.out.println("获取到读锁======读");
        System.out.println("释放读======读");
        rwLock.readLock().unlock();*/








    }

}
